package com.csd.view;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.csd.activity.R;

public class CsdPopupwindow extends PopupWindow {

	private View conentView;
	private TextView text,v_text;
	private String[] str;
	private Activity context;

	public CsdPopupwindow(final Activity context, final TextView text,final String str[],final TextView v_text,int width) {
		this.text = text;
		this.str = str;
		this.context = context;
		this.v_text = v_text;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.pop_listview, null);
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(width);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);
		
	}
	
	public void showList(){
		ListView list = (ListView) conentView.findViewById(R.id.lv_list);
		List<String> listStr = new ArrayList<>();
		for (int i = 0; i < str.length; i++) {
			listStr.add(str[i]);
		}
		list.setAdapter(new PopAdapter(context, listStr));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				text.setText(str[position]);
	               CsdPopupwindow.this.dismiss();
	               if(v_text != null){
	            	   v_text.setVisibility(View.VISIBLE);
	               }
			}
		});
	}

	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, text.getWidth(), 0);
		} else {
			this.dismiss();
		}
	}
	  

	public class PopAdapter extends BaseAdapter{
		private Context context;
		private List<String> list;
		
		public PopAdapter(Context context, List<String> list) {
			super();
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = null;
			ViewHolder holder;
			if(convertView == null){
				view = LayoutInflater.from(context).inflate(R.layout.pop_item, null);
				holder = new ViewHolder();
				view.setTag(holder);
			}else{
				view = convertView;
				holder = (ViewHolder) view.getTag();
			}
			TextView text = (TextView) view.findViewById(R.id.tv_list);
			holder.text = text;
			String str = list.get(position);
			holder.text.setText(str);
					
			return view;
		}
		
		private class ViewHolder{
			TextView text;
		}
		
	}
}
